package com.gu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gu.entity.EduCourse;
import com.gu.entity.vo.CourseInfoVo;
import com.gu.entity.vo.CoursePublishVo;
import com.gu.entity.vo.CourseQuery;
import com.gu.service.EduCourseService;
import com.gu.service.EduTeacherService;
import entity.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Description:  课程信息组件
 *
 * @Autor: Rolin
 * datetime: 05-21-15:47
 */
@RestController
@Api(tags = "课程信息管理")
@CrossOrigin
@RequestMapping("/eduService/course")
public class EduCourseController {

    @Resource
    private EduCourseService eduCourseService;

    @Resource
    private EduTeacherService eduTeacherService;

    @ApiOperation("分页条件查询课程列表")
    @PostMapping("/getCourseListPage/{current}/{limit}")
    public CommonResult getCourseListPage(
            @PathVariable("current") Long current,
            @PathVariable("limit") Long limit,
            @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> courseParams = eduCourseService.getCourseListPage(courseQuery, current, limit);
        List<EduCourse> courseList = courseParams.getRecords();
        long total = courseParams.getTotal();
        return courseList.size() > 0 ? CommonResult.success().data("rows", total).data("courseList", courseList) : CommonResult.error();

    }

    @ApiOperation("根据课程Id删除相对课程")
    @DeleteMapping("/deleteCourseById/{id}")
    public CommonResult deleteCourseById(@PathVariable("id")String courseId){
           boolean result =  eduCourseService.deleteCourseById(courseId);
           return result? CommonResult.success().data("result",true):CommonResult.error().data("result",false) ;
    }

    @PostMapping("/addCourse")
    @ApiOperation(value = "添加课程信息")
    //@RequestBody 对象接受json格式的数据封装到对象中
    public CommonResult addCourse(@RequestBody CourseInfoVo courseInfoVo) {

        String courseId = eduCourseService.saveOrUpdateCourse(courseInfoVo);
        return courseId != "" ? CommonResult.success().data("courseId", courseId) : CommonResult.error();
    }


    @GetMapping("/getCourseById/{id}")
    @ApiOperation(value = "根据Id查询课程")
    public CommonResult getCourseById(@PathVariable("id") String courseId) {

        CourseInfoVo CourseInfoVo = eduCourseService.getCourseById(courseId);
        return CourseInfoVo != null ? CommonResult.success().data("bean", CourseInfoVo) : CommonResult.error();
    }

    @ApiOperation("多表联查回显课程发布内容")
    @GetMapping("/getPublishInfo/{courseId}")
    public CommonResult getPublishInfoByCourseId(@PathVariable("courseId") String courseId) {

        CoursePublishVo coursePublishVo = eduCourseService.getCoursePublishInfoById(courseId);
        return !StringUtils.isEmpty(coursePublishVo.getTitle()) ? CommonResult.success()
                .data("coursePublishVo", coursePublishVo) : CommonResult.error();
    }

    @ApiOperation("课程最终发布")
    @PutMapping("updatePublishInfo/{courseId}")
    public CommonResult updatePublishInfoByCourseId(@PathVariable("courseId") String courseId) {
        //增加乐观锁：
        EduCourse course = eduCourseService.getById(courseId);

        course.setStatus("Normal");
        course.setId(courseId);
        boolean result = eduCourseService.updateById(course);
        return result ? CommonResult.success().data("result", true) : CommonResult.error().data("result", false);
    }

    @ApiOperation("订单页面远程调用专用")
    @GetMapping("/getCourse/{id}")
    public Map<String,Object> getCourseForOrder(@PathVariable("id")String courseId){
        EduCourse eduCourse = eduCourseService.getById(courseId);
        String course_cover = eduCourse.getCover();
        String teacherId = eduCourse.getTeacherId();
        String teacher_name = eduTeacherService.getById(teacherId).getName();
        String course_title = eduCourse.getTitle();

        Map<String, Object> map = new HashMap<>();
        map.put("course_id",courseId);
        map.put("course_cover",course_cover);
        map.put("teacher_name",teacher_name);
        map.put("course_title",course_title);

        return map ;

    }
}
